//
//  dian_ji_Tools_AddReProportMJ_SViewController.m
//  togetherPlay
//
//  Created by Mac on 2019/2/26.
//  Copyright © 2019 rookie_game.qq.com. All rights reserved.
//

#import "dian_ji_Tools_AddReProportMJ_SViewController.h"
#import "STTools_TextView.h"
#import "STTools_ImageAutoChoseView.h"
@interface dian_ji_Tools_AddReProportMJ_SViewController ()
@property(nonatomic, strong) STTools_TextView                     *textView;/**<  */
@property(nonatomic, strong) STTools_ImageAutoChoseView                     *imageAutoView;/**<  */
@property(nonatomic, strong) NSMutableArray                     *imageArray;/**<  */
@end

@implementation dian_ji_Tools_AddReProportMJ_SViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"举报";
    __weak typeof(self) weakSelf =  self;
     self.imageAutoView = [[STTools_ImageAutoChoseView alloc] initWithFrame:CGRectMake(10, 0, UISCREEN_WIDTH - 20, 100)];
    [self.imageAutoView setFrameDidChangedHandle:^(STTools_ImageAutoChoseView *autochoseView) {
        [weakSelf configHeaderView];
    }];
    self.imageAutoView.maxCount = 4;
    [self configHeaderView];
    [self dian_ji_Tools_ui_config_subview];
    
    // Do any additional setup after loading the view.
}
- (void)configHeaderView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 100)];
    header.backgroundColor = UIColor.whiteColor;
    UIView * titleView =  [TMUtils stoneobs_fun_headerViewWithTitle:@"举报图片(必选)"];
    [header addSubview:titleView];
    
   
    self.imageAutoView.top = titleView.bottom;
    [header addSubview:self.imageAutoView];
    header.height = self.imageAutoView.bottom+10;
    self.tableView.tableHeaderView = header;
    
}
#pragma mark --subView
- (void)dian_ji_Tools_ui_config_subview{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 100)];
    footer.backgroundColor = UIColor.whiteColor;
    
    self.textView = [[STTools_TextView alloc] initWithFrame:CGRectMake(10, 10, UISCREEN_WIDTH - 20, 140)];
    self.textView.backgroundColor = self.tableView.backgroundColor;
    self.textView.placeholder = @"请输入举报内容";
    [footer addSubview:self.textView];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, self.textView.bottom + 20, UISCREEN_WIDTH - 60 , 44)
                                                        title:@"举报"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:4
                                              backgroundColor:FlatYellow
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedReportButton];
    }];
    footer.height = confimButton.bottom + 20;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (void)onSelctedReportButton{
    if (!self.textView.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入原因"];
        return;
    }
    self.imageArray = NSMutableArray.new;
    if (self.imageAutoView.stoneobs_fun_get_allImages.count) {
        [SVProgressHUD showWithStatus:@"请稍后"];
        self.imageArray = NSMutableArray.new;
        [SVProgressHUD showWithStatus:@"正在加载"];
        [TMQNUploadManger.manger uploadMoreImage:self.imageAutoView.stoneobs_fun_get_allImages handle:^(BOOL success, NSArray * _Nonnull array) {
            self.imageArray = array;
            [SVProgressHUD dismiss];
            [self sendReportRequest];
        }];
        
    }else{
        [SVProgressHUD showInfoWithStatus:@"请选择图片"];
        return;
        [self sendReportRequest];
    }
}
#pragma mark --NetWork Method
- (void)sendReportRequest{
    [SVProgressHUD showWithStatus:@"正在加载"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    
    if (self.to_id.length) {
        [stoneobs_net_param_dic setObject:self.to_id forKey:@"posts_id"];
    }
    if (self.textView.text.length) {
        [stoneobs_net_param_dic setObject:self.textView.text forKey:@"content"];
    }
    if (self.imageArray.count) {
        NSString * value = [self.imageArray componentsJoinedByString:@","];
        value = self.imageArray.mj_JSONString;
        [stoneobs_net_param_dic setObject:value forKey:@"imgs"];
    }
    
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/posts/report"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        [SVProgressHUD showSuccessWithStatus:@"举报成功"];
        [self.navigationController  popViewControllerAnimated:1];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}

@end
